﻿using CloudStorage_Backend.model;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace CloudStorage_Backend.utils
{
    public class JwtHelper
    {
        public static string GetToken(User user)
        {
            string issuer = "smq";
            string audience = "jwt-audience";
            string tockenSecrete = "123456polkimnjuy78";

            // 密钥
            var securityKey = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tockenSecrete)), SecurityAlgorithms.HmacSha256);
            var claims = new Claim[]
            {
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim(ClaimTypes.Role, ""),
            };
            SecurityToken securityToken = new JwtSecurityToken(
                issuer: issuer,
                audience: audience,
                signingCredentials: securityKey,
                expires: DateTime.Now.AddHours(12),
                claims: claims
                );
            return new JwtSecurityTokenHandler().WriteToken(securityToken);
        }
    }
}
